package com.hod.reservgrid.client.model;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.user.client.Window;

public class RoomTypeRecordset {
	
	public static boolean increaseRoomTypeCapacity(Integer idRoomType){
	    GearsDataStore dsRoomType = (GearsDataStore) GWT.create(RoomType.class);
	    RoomType data;
		try {
			data = (RoomType) dsRoomType.findById(idRoomType);
			data.setNumberOfRooms(data.getNumberOfRooms()+1);
			dsRoomType.save(data);
			//dsRoomType.close();
			return true;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return false;
		}
	      
   }
   
   public static boolean decreaseRoomTypeCapacity(Integer idRoomType){
	    GearsDataStore dsRoomType = (GearsDataStore) GWT.create(RoomType.class);
	    RoomType data;
	    Boolean success = false;
		try {
			data = (RoomType) dsRoomType.findById(idRoomType);
			//Solamente decremento si el numero de habitaciones es mayor a cero
			if (data.getNumberOfRooms()>0){
			   data.setNumberOfRooms(data.getNumberOfRooms()-1);
			   dsRoomType.save(data);
			   success = true;
			}
			//dsRoomType.close();
			return success;
			
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return false;
		}
	      
  }
    
   public static RoomType[] getRoomTypes(){
    		try {
    			GearsDataStore dsRoomType = (GearsDataStore) GWT.create(RoomType.class);
    			GearsQuery q = dsRoomType.getNamedQuery(RoomType.FIND_ALL);
    			RoomType[] data = (RoomType[]) q.execute();
    			//dsRoomType.close();
    			return data;
    		} catch (DatabaseException e) {
    			// TODO Auto-generated catch block
    			return null;
    		} 
     }
   
   public static RoomType getRoomType(Integer idRoomType) {
		// TODO Auto-generated method stub
		GearsDataStore ds = (GearsDataStore) GWT.create(RoomType.class);
		RoomType rt;
		try {
			rt = (RoomType) ds.findById(idRoomType);
			Window.alert("Existe el RoomType");
			return rt;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return null;
		}		
	}

}
